home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / localloginpw / lout.c < prev    next >
C/C++ Source or Header  |  1995-11-05  |  5KB  |  180 lines

  1. //---------------------------------------------------------------------------
  2. // LOGOUT                                        (c) Laurence Vanhelsuwe 1994
  3. // ------
  4. // This program is part of a group of 3 complimentary programs to log the
  5. // usage of a stand-alone machine. (LOGIN, LOGOUT, PASSWORD)
  6. //
  7. // This program has been written in ANSI C to make it fully portable across
  8. // different hardware platforms.
  9. //
  10. // Original development and testing was carried out on a Commodore Amiga 3000.
  11. //
  12. // History
  13. // -------
  14. // 01-JAN-94: Design and initial file creation.
  15. //
  16. //
  17. //---------------------------------------------------------------------------
  18.  
  19. //#define        DEBUG            1
  20.  
  21. #include    "stdio.h"
  22. #include    "stdlib.h"
  23. #include    "string.h"
  24. #include    "fcntl.h"
  25. #include    "time.h"
  26.  
  27. #define        MAX_NAME_LEN    (10)
  28. #define        ENTRY_LEN        (65)
  29.  
  30. #ifdef    AMIGA
  31. #define        LFILE            "DEVS:USERLOG.DAT"
  32. #define        PFILE            "DEVS:PASSWORDS.DAT"
  33. #define        HELP            "?"
  34. #define        OPEN_MODE        (O_RDWR)
  35.  
  36. #else
  37.  
  38. #define        LFILE            "C:/DOS/USERLOG.DAT"
  39. #define        PFILE            "C:/DOS/PASSWOR.DAT"
  40. #define        HELP            "/?"
  41. #define        OPEN_MODE        (O_RDWR | O_BINARY)
  42.  
  43. #endif
  44.  
  45. //---------------------------------------------------------------------------
  46. // Function Prototypes
  47. // -------------------
  48. //---------------------------------------------------------------------------
  49.  
  50. void get_log_entry(void);
  51. void change_log_entry(void);
  52.  
  53. //---------------------------------------------------------------------------
  54. // Globals
  55. // -------
  56. //---------------------------------------------------------------------------
  57.  
  58. char *entry_buffer="X AAAAAAAAAA DDD MMM dd hh:mm:ss YYYY - DDD MMM dd hh:mm:ss YYYY\r";
  59. char name[MAX_NAME_LEN+2];
  60.  
  61. long int file_size, log_pos;
  62. int error, num_entries;
  63.  
  64. int logfile, passwfile;            // LEVEL 1 ANSI C File Handles
  65.  
  66. time_t    systime;
  67. struct tm *tim;
  68.  
  69. //---------------------------------------------------------------------------
  70. //---------------------------------------------------------------------------
  71.  
  72. main(int argc, char **argv) {
  73.  
  74. char *ptr, ch;
  75. int len;
  76.  
  77.     if(argc > 1 ) {
  78.         printf("LOGIN V1.0  (Copyright (c) Jan 1994 L. Vanhelsuwe)\n\n");
  79.         printf("Programmed by Laurence Vanhelsuwe in C on an Amiga 3000.\n");
  80.         printf("Ported to IBM PC environment by L. Vanhelsuwe.\n\n");
  81.         printf("USAGE: LOGOUT [%s]\n\n", HELP);
  82.     }
  83.  
  84.     if ((passwfile = open(PFILE, OPEN_MODE)) == -1L) {
  85.         fprintf(stderr, "Passwords file does not exist !\n");
  86.         exit(10);
  87.     }
  88.     close(passwfile);
  89.  
  90.     if ((  logfile = open(LFILE, OPEN_MODE)) == -1L) {
  91.         fprintf(stderr, "Log file does not exist !\n");
  92.         exit(10);
  93.     }
  94.  
  95.     get_log_entry();                    // get last log entry in entry_buffer
  96.  
  97.     if (entry_buffer[0] == 'I') {
  98.         entry_buffer[0]  = 'O';            // set entry to correctly logged off
  99.  
  100.         time(&systime);                        // get machine local time
  101.         tim = localtime(&systime);            // convert to universal time format
  102.         strncpy(&entry_buffer[40], asctime(tim), 24);    // copy to log file line
  103.  
  104.         ptr = &entry_buffer[3];            // find length of user's name
  105.         len = 1;
  106.         while (len < MAX_NAME_LEN) {
  107.             if (*ptr++ != ' ') len++;
  108.             else break;
  109.         }
  110.  
  111.         strncpy(name, &entry_buffer[2], len);    // maximum MAX_NAME_LEN chars
  112.  
  113.         printf("\n");
  114.         printf("User '%s' Logging out...\n\n", name);
  115.  
  116.         printf("Session duration:\n");    ch = entry_buffer[37]; entry_buffer[37]=0;
  117.         printf("LOGIN  : %s\n", &entry_buffer[13]);
  118.         printf("LOGOUT : %s\n", &entry_buffer[40]);
  119.         printf("\n"); entry_buffer[37] = ch;
  120.  
  121.         change_log_entry();
  122.  
  123.     } else {
  124.         printf("\n");
  125.         printf("  -- WARNING --\n\n");
  126.         printf("No user currently logged in ! (Please use LOGIN)\n");
  127.     }
  128. }
  129.  
  130. //---------------------------------------------------------------------------
  131. // Seek to the last entry and read it in our fixed buffer.
  132. //---------------------------------------------------------------------------
  133. void get_log_entry(void) {
  134. int chars,i;
  135.  
  136.     lseek(logfile, 0L, SEEK_END); file_size = tell(logfile);
  137.  
  138.     num_entries = file_size/ENTRY_LEN;
  139.     log_pos = (num_entries-1)*ENTRY_LEN;
  140.  
  141. #ifdef DEBUG
  142.     printf("File size = %ld, Num Entries = %d, LOG Position = %d, ENTRY_LEN = %d\n",
  143.              file_size, num_entries, log_pos, ENTRY_LEN);
  144. #endif
  145.  
  146.     lseek(logfile, log_pos, SEEK_SET);
  147.     chars = read (logfile, (void*) entry_buffer, ENTRY_LEN);
  148.  
  149. #ifdef DEBUG
  150.     for (i=0; i< 16; i++)
  151.     printf("buf[i] = '%c'    buf[i+16] = '%c'    buf[i+32] = '%c'    buf[i+48] = '%c'\n",
  152.      entry_buffer[i],
  153.      entry_buffer[i+16],
  154.      entry_buffer[i+32],
  155.      entry_buffer[i+48]);
  156.     printf("BUF[64]='%c'    BUF[65]='%c'\n", entry_buffer[64], entry_buffer[65]);
  157.  
  158.     if (chars != ENTRY_LEN) {
  159.         printf("ERROR: get_log_entry() read didn't read %d chars (but %d!)\n", ENTRY_LEN, chars);
  160.         exit(10);
  161.     }
  162. #endif
  163.  
  164.     entry_buffer[12] = 0;            // zero-terminate user field
  165. }
  166. //---------------------------------------------------------------------------
  167. // Seek back to the start of the last entry and overwrite it with the updated
  168. // log entry.
  169. //---------------------------------------------------------------------------
  170. void change_log_entry(void) {
  171.  
  172.     entry_buffer[12] = ' ';            // restore space after name
  173.  
  174.     lseek(logfile, log_pos, SEEK_SET);
  175.     write(logfile, (void*) entry_buffer, ENTRY_LEN);
  176.     close(logfile);
  177. }
  178. //---------------------------------------------------------------------------
  179. //---------------------------------------------------------------------------
  180.